--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:47:35
--
-- RewardView

local TabsBtn = require("app.widget.TabsBtn")
local table_insert = table.insert

local RewardWelfarePanel = import(".RewardWelfarePanel")
local RewardSignPanel = import(".RewardSignPanel")
local RewardThreeDayPanel = import(".RewardThreeDayPanel")
--local RewardInvestPanel = import(".RewardInvestPanel")
--local RewardFamePanel = import(".RewardFamePanel")
--local RewardMemberPanel = import(".RewardMemberPanel")
--local RewardCodePanel = import(".RewardCodePanel")
--local RewardLoginPanel = import(".RewardLoginPanel")
--local RewardLevelPanel = import(".RewardLevelPanel")
--local RewardOnlinePanel = import(".RewardOnlinePanel")
local RewardFatePanel = import(".RewardFatePanel")
local RewardCarnivalPanel = import(".RewardCarnivalPanel")
local RewardDrawPanel = import(".RewardDrawPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.RewardScene

function ClassRef:init()
	self._welfarePanel = self._ui["Panel/welfare"] --福利界面
	self._welfarePanel:setVisible(false)
	self._carnivalPanel = self._ui["Panel/carnival"] --嘉年华
	self._carnivalPanel:setVisible(false)
	self._intoPanel = self._ui["Panel/into"] --登陆送
	self._intoPanel:setVisible(false)
	self._fatePanel = self._ui["Panel/fate"] --命运石板
	self._fatePanel:setVisible(false)
	self._drawPanel = self._ui["Panel/draw"] --抽卡
	self._drawPanel:setVisible(false)

	self._binds = {}

	--初始化的时候传入的值需要过滤 分为右边显示的标签页
	self._btns = {}
	self._tabBtnPosY = {}
	for i = 1, 5 do
		self._btns[i] = self._ui["Panel_tab/selected_btn_"..i]
		self._btns[i]:setVisible(false)
		table_insert(self._tabBtnPosY,self._btns[i]:getPositionY())
	end

	--btn的顺序与枚举的顺序一致
	local sortData = {}
	if self._data[Enums.ActivityType.Welfare] then
		sortData[1]=self._data[Enums.ActivityType.Welfare]
	end
	if self._data[Enums.ActivityType.Fate] then
		sortData[2]=self._data[Enums.ActivityType.Fate]
	end

	if self._data[Enums.ActivityType.Carnival] then
		sortData[3]=self._data[Enums.ActivityType.Carnival]
	end
	if self._data[Enums.ActivityType.Into] then
		sortData[4]=self._data[Enums.ActivityType.Into]
	end
	if self._data[Enums.ActivityType.Draw] then
		sortData[5]=self._data[Enums.ActivityType.Draw]
	end
	self._data = sortData
	self._sortTypeTable = {Enums.ActivityType.Welfare,Enums.ActivityType.Fate,Enums.ActivityType.Carnival,Enums.ActivityType.Into,Enums.ActivityType.Draw}

	for k,v in pairs(self._data) do
		if k >= 1 and k <= 5 then
			if self._sortTypeTable[k] ~= Enums.ActivityType.Welfare then
				self._btns[k]:getChildByName("name_1"):setString(L(v.name))
				self._btns[k]:getChildByName("name_2"):setString(L(v.name))
			else
				self._btns[k]:getChildByName("name_1"):setString(L("activityRightTitle"))
				self._btns[k]:getChildByName("name_2"):setString(L("activityRightTitle"))
			end
			self._btns[k]:setVisible(true)
		end
	end
	--客户端这边需要更改顺序

	self:_adjustPos()

	--福利、命运石板、嘉年华、登录送、命运卡牌
	local function onTabsBtn(index, btn)
		--这index为btn的顺序 同时也与枚举一致
		if index then
			if self._curSelectedType then
				--self._curSelectedType = index

				if self._sortTypeTable[index] ~= Enums.ActivityType.Welfare then
					local data = self._data[index]
					self:switchPanel(data.uuid,self._sortTypeTable[index],data.subType)
				else
					local data = self._data[index][1]
					self:switchPanel(data.uuid,self._sortTypeTable[index],data.subType)
				end
			else
				self._curSelectedType = self._sortTypeTable[index]
			end
		end
	end

	self._tabsBtn = TabsBtn.new(self._btns, onTabsBtn, 1)
    self._ui["Panel/Button_close"]:addClickEventListener(function()
    		executeFunc(self._onCloseView)
		end)
		
	app:sendMsg("RewardModel", "getFateStoneModel"):reqRewards()
end

function ClassRef:refreshRedPoint(allActivityRedPoint)
	local function showRedPoint(idx, show)
		if self._btns[idx] then
			self._btns[idx]:getChildByName("dot"):setVisible(show)
		end
	end
	for k,v in pairs(self._data) do
		if self._sortTypeTable[k] ~= Enums.ActivityType.Welfare then
			-- if allActivityRedPoint[v.uuid] then
			--     self._btns[k]:getChildByName("dot"):setVisible(true)
			-- else
			--     self._btns[k]:getChildByName("dot"):setVisible(false)
			-- end
			showRedPoint(k, allActivityRedPoint[v.uuid])
		else
			local welfareInfos = self._data[k]
			if welfareInfos then
				local welfare = false
				for _, wel in ipairs(welfareInfos) do
					if allActivityRedPoint[wel.uuid] then
						welfare = true
					end
				end
				showRedPoint(k, welfare)
				--如果现在打开的是福利 左侧红点也要刷新
				-- self._btns[k]:getChildByName("dot"):setVisible(welfare)
			end
		end
	end

	if self._curSelectedType == Enums.ActivityType.Welfare then
		local panel = self:getPanelById(Enums.ActivityType.Welfare)
		if panel then
			panel:refreshLeftListRedPoint(allActivityRedPoint)
		end
	end
end

--调整右边按钮的位置 总共5个按钮
function ClassRef:_adjustPos()
	-- 以下：动态调整posY
	local visibleBtnNum = 0 -- 可见按钮个数
	for i = 1, #self._tabBtnPosY do
		if self._ui["Panel_tab/selected_btn_"..i]:isVisible() then
			visibleBtnNum = visibleBtnNum + 1
			self._ui["Panel_tab/selected_btn_"..i]:setPositionY( self._tabBtnPosY[visibleBtnNum] )
		end
	end
end


--这里作为外部接口 即点击主界面的入口进来的时候传入的数据
function ClassRef:show(data)
	self.super.show(self)
	if data then
		self:switchPanel(data.uuid,data.type,data.subType)
	end
end

function ClassRef:onSwitchPanel(switchPanelFunc)
	self._switchPanelFunc = switchPanelFunc
end

function ClassRef:switchPanel(uuid,type,subType)
	if type then
		self._curSelectedType = type
		--需要从type反推index
		local selectedIdx = 1
		for k,v in ipairs(self._sortTypeTable) do
			if v == type then
				selectedIdx = k
				break
			end
		end


		self._tabsBtn:setSelected(selectedIdx)
	end
	--此处增加子类别
	self._curSubType = subType

	self:createPanel(self._curSelectedType)

	executeFunc(self._onSwitchPanel,uuid,self._curSelectedType,self._curSubType)

	for k,v in pairs(self._binds) do
		if v then
			if tonumber(k) == self._curSelectedType then
				v:setVisible(true)
			else
				v:setVisible(false)
			end
		end
	end
end

function ClassRef:createPanel(index)
	if not self._binds[index] then
		local panel
		if index == Enums.ActivityType.Fate then
			panel = RewardFatePanel.create(nil, self._fatePanel)
		elseif index == Enums.ActivityType.Into then
			panel = RewardThreeDayPanel.create(self._intoPanel)
			panel:setSubType(self._curSubType)
		elseif index == Enums.ActivityType.Carnival then
			panel = RewardCarnivalPanel.create(nil, self._carnivalPanel)
		elseif index == Enums.ActivityType.Welfare then
			panel = RewardWelfarePanel.create(self._welfarePanel)
		elseif index == Enums.ActivityType.Draw then
			panel = RewardDrawPanel.create(nil, self._drawPanel)
		end
		if panel then
			if panel.onOperate then
				panel:onOperate(self._onOperate)
			end
			self._binds[index] = panel
		end
	else
		self._binds[index]:onOperate(self._onOperate)
	end
end

function ClassRef:getPanelById( idx )
	return self._binds[ idx ]
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end

function ClassRef:refreshCurrentPanel(data)
	local panel = self:getPanelById(self._curSelectedType)
	if panel then
		panel:setData(data)
	end
end

function ClassRef:initWelfareLeftList(listData,curActivityId,allActivityRedPoint)
	local panel = self:getPanelById(Enums.ActivityType.Welfare)
	if panel then
		panel:initLeftList(listData,curActivityId)
		panel:refreshLeftListRedPoint(allActivityRedPoint)
	end
end

function ClassRef:refreshRecommendPanel()
	local panel = self:getPanelById(Enums.ActivityType.Welfare)
	if panel then
		panel:refreshRecommendPanel()
	end
end

function ClassRef:refreshAmazingAwardPanel(param, type)
	local panel = self:getPanelById(Enums.ActivityType.Welfare)
	if panel then
		panel:refreshAmazingAwardPanel(param, type)
	end
end

function ClassRef:updateAmazingAwardMoney()
	local panel = self:getPanelById(Enums.ActivityType.Welfare)
	if panel then
		panel:updateAmazingAwardMoney()
	end
end

function ClassRef:updateThoughLvPanel()
	local panel = self:getPanelById(Enums.ActivityType.Welfare)
	if panel then
		panel:updateThoughLvPanel()
	end
end

return ClassRef